package com.onLineCar.apply.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.onLineCar.apply.domain.Apply;
import com.onLineCar.apply.mapper.ApplyMapper;
import com.onLineCar.apply.service.IApplyService;
import com.onLineCar.common.core.utils.DateUtils;
import com.onLineCar.common.security.utils.SecurityUtils;
import com.onLineCar.common.system.domain.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * app更新Service业务层处理
 *
 * @author godGuang
 * @date 2023-12-24
 */
@Service
public class ApplyServiceImpl implements IApplyService {
    @Autowired
    private ApplyMapper applyMapper;

    public static final String WINDOWS_TITLE = "Windows";
    public static final String IPHONE_TITLE = "iPhone";
    public static final String ANDROID_TITLE = "Android";
    public static final String DRIVER_NAME = "driver";
    public static final String PASSENGER_NAME = "passenger";
    public static final String USER_AGENT = "User-Agent";

    public static final int APPLY_ID = 0;

    /**
     * 查询app更新
     *
     * @param applyId app更新主键
     * @return app更新
     */
    @Override
    public Apply selectApplyByApplyId(Long applyId) {
        return applyMapper.selectApplyByApplyId(applyId);
    }

    /**
     * 查询app更新列表
     *
     * @param apply app更新
     * @return app更新
     */
    @Override
    public List<Apply> selectApplyList(Apply apply) {
        return applyMapper.selectApplyList(apply);
    }

    /**
     * 新增app更新
     *
     * @param apply app更新
     * @return 结果
     */
    @Override
    public int insertApply(Apply apply) {
        apply.setCreateTime(DateUtils.getNowDate());
        String applyPackage = apply.getApplyPackage();
        try {
            // 创建URI对象
            URI uri = new URI(applyPackage);
            // 获取路径部分
            String path = uri.getPath();
            String substring = path.substring(1);
            System.out.println(substring);
            apply.setApplyPackage(substring);
            return applyMapper.insertApply(apply);
        }catch (URISyntaxException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 修改app更新
     *
     * @param apply app更新
     * @return 结果
     */
    @Override
    public int updateApply(Apply apply) {
        apply.setUpdateTime(DateUtils.getNowDate());
        return applyMapper.updateApply(apply);
    }

    /**
     * 批量删除app更新
     *
     * @param applyIds 需要删除的app更新主键
     * @return 结果
     */
    @Override
    public int deleteApplyByApplyIds(Long[] applyIds) {
        return applyMapper.deleteApplyByApplyIds(applyIds);
    }

    /**
     * 删除app更新信息
     *
     * @param applyId app更新主键
     * @return 结果
     */
    @Override
    public int deleteApplyByApplyId(Long applyId) {
        return applyMapper.deleteApplyByApplyId(applyId);
    }

    @Override
    public Apply versionUpdate(HttpServletRequest request) {
        //获取到请求头中的User-Agent
        //判断登录的是什么系统
        String userAgent = request.getHeader(USER_AGENT);
        // 判断登录的是什么系统，如果是 Windows 则不需要更新
        if (userAgent.contains(WINDOWS_TITLE)) {
            return new Apply();
        }
        Integer updateSystemId = null;
        // 判断用户系统类型
        updateSystemId = userAgent.contains(IPHONE_TITLE) ? 1 : (userAgent.contains(ANDROID_TITLE) ? 2 : null);
        // 构建 LambdaQueryWrapper 进行数据库查询
        LambdaQueryWrapper<Apply> lambdaedQuery = Wrappers.lambdaQuery();
        lambdaedQuery.eq(Apply::getApplyUpdateSystemId, updateSystemId).orderByDesc(Apply::getApplyStartTime).last("limit 1");
        // 执行数据库查询
        List<Apply> applies = applyMapper.selectList(lambdaedQuery);
        // 如果查询结果不为空，则进行版本检查
        if (!applies.isEmpty()) {
            Apply newApply = applies.get(APPLY_ID);
            // 获取登录用户的信息
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (!loginUser.getSysUser().getVersion().equals(newApply.getApplyNumber())) {
                return newApply;
            }
        }
        return new Apply();
    }

}
